package com.longnuoqi.mydoctor.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.longnuoqi.mydoctor.activity.ZizhenDiseaseActivity;
import com.longnuoqi.mydoctor.entity.Disease;


import java.util.List;


/**
 * Created by Lenovo on 2016/1/3.
 */
public class ExpandableListUtil {
    public static void expandableList(final Context context, ExpandableListView listView, final List<List<Disease>> list, final String[] category) {
        final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            //定义一个显示文字信息的方法
            TextView getTextView() {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 120);
                TextView textView = new TextView(context);
                //设置 textView控件的布局
                textView.setLayoutParams(lp);
                //设置该textView中的内容相对于textView的位置
                textView.setGravity(Gravity.CENTER);
                //设置txtView的内边距
                textView.setPadding(0, 0, 0, 0);
                //设置文本颜色
                textView.setTextColor(Color.DKGRAY);
                return textView;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return true;
            }

            //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                //定义一个LinearLayout用于存放ImageView、TextView
                LinearLayout ll = new LinearLayout(context);
                //设置子控件的显示方式为水平
                ll.setOrientation(LinearLayout.HORIZONTAL);
                TextView textView = getTextView();
                textView.setTextSize(14);
                textView.setText(category[groupPosition]);
                ll.addView(textView);
                return ll;
            }

            //取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID（分组及子项目的ID）.
            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }

            //取得分组数
            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return category.length;
            }

            //取得与给定分组关联的数据
            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return category[groupPosition];
            }

            //取得指定分组的子元素数.
            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return list.get(groupPosition).size();
            }

            //取得显示给定分组给定子位置的数据用的视图
            @Override
            public View getChildView(int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub

                //定义一个LinearLayout用于存放ImageView、TextView
                LinearLayout ll = new LinearLayout(context);
                //设置子控件的显示方式为水平
                ll.setOrientation(LinearLayout.HORIZONTAL);
                TextView textView = getTextView();
                textView.setTextSize(14);
                textView.setPadding(60, 10, 10, 0);
                textView.setText(list.get(groupPosition).get(childPosition).getName());
                ll.addView(textView);
                return ll;

            }

            //取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID（分组及子项目的ID）.
            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return childPosition;
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return list.get(groupPosition).get(childPosition).getName();
            }
        };
        listView.setAdapter(adapter);
        //为ExpandableListView的子列表单击事件设置监听器
        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(context, ZizhenDiseaseActivity.class);
                intent.putExtra("id", list.get(groupPosition).get(childPosition).getId());
                int diseaseId = list.get(groupPosition).get(childPosition).getId();
                Log.e("======!!!!!!!!!", "disease address:" + "http://www.tngou.net/api/disease/show?id=" + diseaseId);
                context.startActivity(intent);
                return true;
            }
        });
    }
}
